package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.concurrent.FutureTask;

public interface IFunc<TResult> extends IDelegate {
    /*
    + invoke():void
+default  invokeAsync(@nullable AsyncDelegatesCallback callback):FutureTask<TResult>
    * */

    TResult invoke() throws Exception;

    default FutureTask<TResult> invokeAsync(@Nullable AsyncCallback callback) {
        FutureTask<TResult> futureTask = new FutureTask<>(() -> {
            TResult result = invoke();
            if (callback != null) {
                callback.invoke(this, new Object[]{result});
            }
            return result;
        });

        ASYNC_DELEGATE_THREAD_POOL.submit(futureTask);
        return futureTask;
    }
}
